package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.mapper.ProductMMapper;
import com.heima.mapper.RegionMapper;
import com.heima.pojo.dto.PartnerQueryDTO;
import com.heima.pojo.dto.RegionDTO;
import com.heima.pojo.entity.TbRegion;
import com.heima.pojo.entity.TbSku;
import com.heima.pojo.vo.RegionVO;
import com.heima.pojo.vo.SkuClassPageVO;
import com.heima.service.RegionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RegionServiceImpl implements RegionService {
    @Autowired
    private RegionMapper regionMapper;

    @Override
    public SkuClassPageVO search(PartnerQueryDTO partnerQueryDTO) {
        PageHelper.startPage(partnerQueryDTO.getPageIndex(), partnerQueryDTO.getPageSize());
        List<RegionVO> records = regionMapper.search(partnerQueryDTO);
        // 获取分页信息
        Page<RegionVO> page = (Page<RegionVO>) records;
        SkuClassPageVO pageVO = new SkuClassPageVO();
        pageVO.setPageIndex(page.getPageNum());
        pageVO.setPageSize( page.getPageSize());
        pageVO.setTotalCount(page.getTotal());
        pageVO.setTotalPage(page.getPages());
        pageVO.setCurrentPageRecords(records);
        return pageVO;

    }

    @Override
    public Boolean saveRegion(RegionDTO regionDTO) {
        TbRegion tbRegion = new TbRegion();
        BeanUtils.copyProperties(regionDTO, tbRegion);
        tbRegion.setName(regionDTO.getRegionName());
        return regionMapper.saveRegion(tbRegion);

    }

    @Override
    public Boolean updateRegion(String regionId, RegionDTO regionDTO) {
        TbRegion tbRegion = new TbRegion();
        BeanUtils.copyProperties(regionDTO, tbRegion);
        tbRegion.setName(regionDTO.getRegionName());
        return regionMapper.updateRegion(regionId, tbRegion);
    }

    @Override
    public Boolean deleteRegion(String regionId) {
        return regionMapper.deleteRegion(regionId);

    }
}
